package cn.rslee.java.demos

/**
 * @author 325175
 *  参考: http://docs.scala-lang.org/cheatsheets/?_ga=1.23227514.1363845503.1446537134
 */
object ScalaDemo {
  //1.Hello Word
  def main(args: Array[String]) {
    println("Hello World!")
  }

  //2.定义变量,用关键字var
  // scala定义变量可以不指定类型，由于scala的编译器比较强悍，可以推断类型
  // 当然你也可以指定类型
  var name = "rslee"
  var age: Int = 28

  //3.定义常量,用关键字val
  val distance = 1000000L

  //4.私有变量
  private var width = 182.03

  //5.判断语句

  def showIf(name: String) {
    if (name == null) {
      println("the name is null")
    } else {
      println("the name:" + name)
    }
  }

  // 6.while 循环
  var x = 1
  while (x < 5) { println(x); x += 1 }

  do { println(x); x -= 1 }
  while (x > 1)

  // 7.for循环

  for (x <- 1 to 100) {
    println(x)
  }
  // 不包含100
  for (x <- 1 until 100) {
    println(x)
  }

  // 打印偶数
  for (x <- 1 to 100 if x % 2 == 0) {
    println(x)
  }

  //同时循环
  for (x <- 1 to 100; y <- 100 to 1) {
    println("%d/%d=%.1f".format(x, y, x * y))
  }

  // break:scala 里面break是一个方法，需要导入
  import scala.util.control.Breaks._
  // ._表示导入所有对象

  for (x <- 1 to 100) {
    if (x >= 60) {
      break
    }
  }

  //8.方法定义
  def f(x: Int) = { x * x }

  def f1(x: Int) = x * x

  def f2(x: Int): Int = {
    x * x
//    return 100
  }

  //匿名函数
  val foo = (x: Int) => x * x
  val foo2 = (x:Int) =>{
    x * x
  }

  foo(100)

  //多参数定义
  def a(x: Int, y: Int) = x + y

  // 柯里化(curried),柯里化的函数可以不一次性传输所有的参数
  def c(x: Int)(y: Int) = x + y

  val c1 = c(_: Int)(3) // 先传入第二个参数
  val c2 = c(4) _ // 先传入第一个参数

  println(c1(4))
  println(c2(3))

  //无参函数
  def d() = println("None")

  //无参函数调用可以不用括号
  d

  // 9.类定义

  class C(age: Int, name: String) {

    def say() = println("My name is " + name + ", age " + age)

  }

  //类继承

  class F {
    def say = println("I'm F")
  }

  class FA extends F {
    // 必须使用override关键字
    override def say = println("I'm FA")
    // 重载
    def say(name: String) = println("I'm " + name)
  }

  //抽象类
  abstract class AC {
    //定义抽象方法

    def say
    def get: String
  }

  class ACS extends AC {
    def say = println("ACS")
    def get(): String = {
      "ACS"
    }
  }

  new ACS().say

  // scala 特有的类型：trait,可以说是个502，扩展已有类的方法,实现AOP的功能

  trait T {
    val name = "rslee"
    def sayName = println("hello " + name)
  }

  class ACT extends ACS with T {}

  new ACT().sayName

  //case class,类似java的pojo,可以用于模式匹配

  case class CC(age: Int, name: String)

  // 10.单例

  object S {

    val age = 28
    def say = println("age " + age)
  }

  S.say

  // 11.模式匹配
  val values = new CC(28, "rslee")

  values match {
    case CC(28, "rslee") => println("got it")
    case _               => println("not match")
  }

  // 12.函数即对象
  //scala实现代理模式
  def proxy(name: String, func: String => Unit) = {
    func(name)
  }

  val p = (x: String) => println("你好，" + x)
  proxy("rslee", p)

  //多参数
  def proxy2(name: String, func: (String, Int) => Unit) = {
    func(name, 182)
  }

  val p2 = (x: String, y: Int) => println("name:" + x + ",age:" + y)

  proxy2("dd", p2)

}